package artid.com.arttogo.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

import artid.com.arttogo.R;
import artid.com.arttogo.bean.NavigationItem;
import artid.com.arttogo.bean.ShareItem;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.AppUrl;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.utils.MySharedPreferences;

/**
 * 广告页面zx
 *
 * @author
 */
public class AdvActivity extends BaseActivity {
    int WAITTIME = 3;
    public static String advTag = "adv";
    public static String savedUrl = "";
    MySharedPreferences sharedPreferences;
    public static File picFile = new File(
            Environment.getExternalStorageDirectory(), "advPic.jpg");
    private RelativeLayout m;
    // 延迟3秒
    private static final long SPLASH_DELAY_MILLIS = 500;

    String shareTime, advTime, whiteListTime, guideTime;
    ImageView mImageView;
    View mPass;
    TextView mTextNum;
    Timer time;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        mContext = AdvActivity.this;
//        requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题
        setContentView(R.layout.adv_layout);
        m = (RelativeLayout) findViewById(R.id.app_back);
        mImageView = (ImageView) findViewById(R.id.adv_img);
        mPass = (View) findViewById(R.id.adv_view);
        mPass.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                jumpToMain();
            }
        });
        mTextNum = (TextView) findViewById(R.id.adv_num);
        time = new Timer();

        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                handler.sendMessage(new Message());

            }
        };

        time.schedule(task, 0, 1000);

        sharedPreferences = MySharedPreferences
                .getMySharedPreferences(mContext);

        loadSavedPic();
        // DelyLog();
        // handler.sendEmptyMessageDelayed(1, SPLASH_DELAY_MILLIS);
    }

    // 先加载历史
    void loadSavedPic() {

        String savedData = sharedPreferences.getValue(advTag, "");
        if (TextUtils.isEmpty(savedData) || !picFile.isFile()) {
            jumpToMain();
            return;
        }
        VolleyData savedVolleyData = new VolleyData(savedData,
                NavigationItem.class);
        final NavigationItem savedItem = (NavigationItem) savedVolleyData.getData();
        savedUrl = savedItem.getPath();
        final String savedtype = savedItem.getAd_type();
        final String id = savedItem.getUrlid();
        Bitmap bmpDefaultPic = null;
        if (bmpDefaultPic == null)
            bmpDefaultPic = BitmapFactory.decodeFile(picFile.getAbsolutePath(),
                    null);
        if (bmpDefaultPic == null) {
            Log.d("没有本地图片", "没有本地图片");
            sharedPreferences.setValue(advTag, "");
            // getAdvBitmap();
            return;
        }
//		bmpDefaultPic =	BlurUtil.doBlur(bmpDefaultPic, 5,10);
        mImageView.setImageBitmap(bmpDefaultPic);
        mImageView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                time.cancel();
                Intent intentMain = new Intent(mContext,
                        MainActivity.class);
                startActivity(intentMain);
                if (savedtype.equals("1")) {
                    Intent intent = new Intent(mContext,
                            CollecterActivity.class);
                    intent.putExtra("id", id);
                    startActivity(intent);
                } else if (savedtype.equals("2")) {
                    Intent intent = new Intent(mContext, WebViewActivity.class);
                    ShareItem si = new ShareItem();

                    si.setId(id);
                    si.setImageUrl(savedUrl);
                    si.setContent(savedItem.getDescription());
                    si.setTitle(savedItem.getTitle());
                    si.setUrl(Cache.base + "h5/forensic-news.html?id=" + id);
                    intent.putExtra("share", si);
                    intent.putExtra("id", id);
                    intent.putExtra("type", AppUrl.AUTH_NEWS);
                    startActivity(intent);
                } else if (savedtype.equals("3")) {
                    Intent intent = new Intent(mContext, WebViewActivity.class);
                    ShareItem si = new ShareItem();
                    si.setId(id);
                    si.setImageUrl(savedUrl);
                    si.setContent(savedItem.getDescription());
                    si.setTitle(savedItem.getTitle());
                    si.setUrl(Cache.base + "h5/exhibition-hall.php?id=" + id);
                    intent.putExtra("share", si);
                    intent.putExtra("id", id);
                    intent.putExtra("type", AppUrl.GALLERISE);
                    startActivity(intent);
                } else if (savedtype.equals("4")) {

                } else if (savedtype.equals("5")) {
                    Intent intentYsp = new Intent(mContext, CollectionDetailPagerActivity.class);
                    intentYsp.putExtra("id", id);
                    intentYsp.putExtra("titleName", savedItem.getTitle());
                    startActivity(intentYsp);
                } else if (savedtype.equals("6")) {
                    Intent intentYsp = new Intent(mContext, WebViewActivity.class);
                    intentYsp.putExtra("type", AppUrl.LOAD_HOMEPAGE_WEB);
                    intentYsp.putExtra("url", savedItem.getShareurl());
                    startActivity(intentYsp);
                } else if (savedtype.equals("7")) {
                    Intent intentmy = new Intent(mContext, CelebritySeal_Activity.class);
                    startActivity(intentmy);
                }
                finish();
            }
        });

    }

    void DelyLog() {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                jumpToMain();
            }
        }, 1500);
    }

    protected Animation getAnimation() {
        // Animation anim = new ScaleAnimation(1.0f, 1.2f, 1.0f, 1.2f,
        // Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
        // 0.5f);
        // anim.setFillAfter(true);
        Animation anim = new Animation() {
        };
        anim.setDuration(1);
        return anim;
    }

    Intent mIntent;
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {

        public void handleMessage(Message msg) {
            mTextNum.setText(String.valueOf(WAITTIME));
            WAITTIME--;
            if (WAITTIME == 0) {
                // time.cancel();
                jumpToMain();
            }

            // if (m==null) {
            // Intent intent = new Intent(SplashActivity.this,
            // MainActivity.class);
            // SplashActivity.this.startActivity(intent);
            // SplashActivity.this.finish();
            // return;
            // }
            // Animation anim = getAnimation();
            //
            // m.startAnimation(anim);
            //
            // anim.setAnimationListener(new AnimationListener() {
            // @Override
            // public void onAnimationStart(Animation animation) {
            //
            // }
            //
            // @Override
            // public void onAnimationRepeat(Animation animation) {
            //
            // }
            //
            // @Override
            // public void onAnimationEnd(Animation animation) {
            // Intent intent = new Intent(SplashActivity.this,
            // MainActivity.class);
            // SplashActivity.this.startActivity(intent);
            // SplashActivity.this.finish();
            // }
            // });
        }

    };

    void jumpToMain() {
        time.cancel();
        Intent intent = new Intent(mContext, MainActivity.class);
        startActivity(intent);
        finish();
    }

    private static void outputLog(String strTAG, String strInfo) {
        Log.i(strTAG, strInfo);
    }
}
